//
//  ShangChengFanLiViewController.m
//  EJL
//
//  Created by adc on 15/11/6.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "ShangChengFanLiViewController.h"
#import "ZyCollectionViewCell.h"
#import "WoDeShangPinTableViewCell.h"
#import "SearchViewController.h"
#import "GoodsInfoVC.h"
#import "SecondPinPaiFenXiao.h"
#import "ShangChengFanLiTableViewCell.h"
@interface ShangChengFanLiViewController ()

@end

@implementation ShangChengFanLiViewController
-(void)viewWillAppear:(BOOL)animated
{
    [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
    [HttpsManager requestPinPaiFenXiao:self.brandDistri categoryID:self.categoryID sub:self.subcategoryID page:@"0" shopID:self.shopID  tab:nil completion:^(PinPaiFenXiao *pinPai) {
        _pinPai=pinPai;
        _dataAry=_pinPai.listAry;
        // NSLog(@"zzz%d",self.good.subId);
        [GMDCircleLoader hideFromView:self.view animated:YES];
        [_tableView reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-75, 7, 150, 30)];
    label.text=self.titleName;
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
   
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10, 7, 30, 30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];

    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 140, WIDE,HEIGHT-140) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    //[_tableView registerNib:[UINib nibWithNibName:@"PinPaiFenXiaoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ShangChengFanLiTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
//    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
//        [_tableView setLayoutMargins:UIEdgeInsetsZero];
//    }
//    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
//        [_tableView setSeparatorInset:UIEdgeInsetsZero];
//    }
    
    for (int i=0; i<5; i++)
    {
        UIButton*pinPaiBut=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/5*i, 64+44, WIDE/5, 44)];
        pinPaiBut.tag=i+1;
        NSArray*ary=[NSArray arrayWithObjects:@"默认", @"销量",@"价格",@"最新",@"佣金",nil];
        [pinPaiBut setTitle:ary[i] forState:UIControlStateNormal];
        [pinPaiBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [pinPaiBut setTitleColor:RGB_COLOR(0x00c49f)forState:UIControlStateSelected];
        if (i==0)
        {
            pinPaiBut.selected=YES;
            _select=pinPaiBut.tag;
            _a=0;
        }
        [pinPaiBut addTarget:self action:@selector(bigButClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:pinPaiBut];

    }
    
    
    
   }


-(void)bigButClick:(UIButton*)sender
{
    UIButton*lastBut=(UIButton*)[self.view viewWithTag:_select];
    lastBut.selected=NO;
    
    sender.selected=YES;
    _select=sender.tag;
    _a=sender.tag-1;
    NSLog(@"----%d",_select);
    
    NSInteger index=sender.tag-1;
    
    
    NSString*str=[NSString stringWithFormat:@"%ld",(long)index];
    [HttpsManager requestPinPaiFenXiao:self.brandDistri categoryID:self.categoryID sub:self.subcategoryID page:@"0" shopID:self.shopID tab:str completion:^(PinPaiFenXiao *pinPai) {
        _pinPai=pinPai;
        _dataAry=_pinPai.listAry;
      
        [_tableView reloadData];
        [GMDCircleLoader hideFromView:self.view animated:YES];
    }];
    

    
    
    
}
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondPinPaiFenXiao*sec=_dataAry[indexPath.row];
    GoodsInfoVC*vc=[[GoodsInfoVC alloc]init];
   
    vc.productId=sec.productId;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
}


- (IBAction)searchBut:(UIButton *)sender
{
    SearchViewController*vc=[[SearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 170;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondPinPaiFenXiao*sec=_dataAry[indexPath.row];
    
//    WoDeShangPinTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" ];
//    if (!cell) {
//        cell = [[NSBundle mainBundle] loadNibNamed:@"WoDeShangPinTableViewCell" owner:nil options:nil][0];
//    }
//    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:sec.imgUrl] placeholderImage:[UIImage imageNamed:@"默认图片"]];
//    
//    
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    return cell;
    ShangChengFanLiTableViewCell*cell1=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell1.picture sd_setImageWithURL:[NSURL URLWithString:sec.imgUrl] placeholderImage:[UIImage imageNamed:@"头像"]];
    NSLog(@"zzzz%@",sec.title);
    cell1.name.text=sec.title;
    if (sec.discounting)
    {
        cell1.shouJia.text=[NSString stringWithFormat:@"%d",sec.price];
    }
    else
    {
         cell1.shouJia.text=[NSString stringWithFormat:@"%d",sec.discountPrice];
    }
    cell1.yongjin.text=[NSString stringWithFormat:@"%d",sec.commision];
    cell1.kuCun.text=[NSString stringWithFormat:@"%d",sec.collectionNum];
    return cell1;
}

//-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
//{
//    return 15;
//}
//-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    ZyCollectionViewCell*coll=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//    return coll;
//}
//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
